package com.yf.menu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		String text = null;
		switch (itemId) {
		case R.id.action_settings:
			text = "action_settings";
			break;
		case R.id.action_settings2:
			text = "action_settings2";
			break;
		case R.id.action_settings3:
			text = "action_settings3";
			break;
		}

		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
		return false;
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		System.out.println(featureId);
		System.out.println(menu);
		// 当用户点击菜单按钮就会执行此方法
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("菜单标题");
		builder.setMessage("菜单内容");
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
		// 如果返回值是true就会走系统默认的实现执行onCreateOptionsMenu
		// return super.onMenuOpened(featureId, menu);
		return false;
	}
}
